package tcm.com.gistone.database.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.test.context.jdbc.Sql;
import tcm.com.gistone.database.entity.Word;

import javax.persistence.PrePersist;
import java.util.List;

public interface WordMapper {
    int deleteByPrimaryKey(Long wordId);

    int insert(Word record);

    int insertSelective(Word record);

    Word selectByPrimaryKey(Long wordId);

    int updateByPrimaryKeySelective(Word record);

    int updateByPrimaryKey(Word record);

    /*根据名称查询*/
    @Select("select * from tb_word where special_id = #{specialId} and  theme_type = #{wordType} and word = #{word}")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    Word selectByWord(@Param("specialId") long specialId, @Param("word") String word, @Param("wordType") long wordType);

    @Select("select * from tb_word where theme_type = #{wordType} and word = #{word} ")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    List<Word> selectByWordAllSpecial(@Param("word") String word, @Param("wordType") long wordType);

    /*按类查询主题词*/
    @Select("select * from tb_word where theme_type = #{type} and word_id = parent_id")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    List<Word> selectByType(long type);

    @Select("select word from tb_word where theme_type = #{type}")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    List<Word> selectNameByType(long type);

    @Select("select theme_type from tb_word where word_id = #{id}")
    long selectTypeById(Long id);

    @Select("select * from tb_word where parent_id = #{id}")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    List<Word> selectByParentId(long id);

    /*查询别名*/
    @Select("select * from tb_word where parent_id = #{id} and word_id != #{id}")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    List<Word> selectAlias(long id);


    @Select("select * from tb_word where special_id = #{specialId} and theme_type = #{type} and word = #{word}")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    List<Word> selectOne(@Param("type") long type, @Param("word") String word,@Param("specialId") long specialId);

    @Select("select * from tb_word where special_id = #{specialId} and theme_type = #{type} and word =  #{word} and parent_id is null")
    @ResultMap("tcm.com.gistone.database.mapper.WordMapper.BaseResultMap")
    List<Word> selectMainWord(@Param("type") long type, @Param("word") String word,@Param("specialId") long specialId);

    @Update("set names utf8mb4;")
    void updateNames();
}